package rss_POJO;

import java.util.Date;
import java.util.Collection;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author RS-team
 */
@Entity
@Table(name = "PLAYLIST")
//@Table(name = "PLAYLIST", uniqueConstraints = {
//    @UniqueConstraint(columnNames = {"NAME"})})
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "PlayList.findAll", query = "SELECT p FROM PlayList p"),
    @NamedQuery(name = "PlayList.findByPlaylistid", query = "SELECT p FROM PlayList p WHERE p.playlistid = :playlistid"),
    @NamedQuery(name = "PlayList.findByName", query = "SELECT p FROM PlayList p WHERE p.name = :name"),
    @NamedQuery(name = "PlayList.findByCreationdate", query = "SELECT p FROM PlayList p WHERE p.creationdate = :creationdate")})
public class PlayList implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PLAYLISTID")  // είναι not nullable αλλά και unique ως primary key
        @Basic(optional = false)
        private Integer playlistid;
    @Column(name = "NAME", nullable = false, unique = true, length = 100)  // μοναδικότητα κύριου αναγνωριστικού πεδίου αναζήτησης
        @Basic(optional = false)
        private String name;
    @Column(name = "CREATIONDATE", nullable = true, unique = false)
        @Basic(optional = true)
        @Temporal(TemporalType.TIMESTAMP)
        private Date creationdate;
    
    @ManyToMany
        @JoinTable(name = "PLAYLIST_SONG", joinColumns = {
            @JoinColumn(name = "PLAYLIST_ID", referencedColumnName = "PLAYLISTID")}, inverseJoinColumns = {
            @JoinColumn(name = "SONG_ID", referencedColumnName = "SONGID")})
        private Collection<Song> songCollection;


    /* default constructor */
    public PlayList() {
    }
    /* "τεμπέλικος" constructor χωρίς τα nullable πεδία */
    public PlayList(String name) {
        this.playlistid = null;  // ετσι κι αλλιώς θα το ορίσει η ΒΔ
        this.name = name;
    }
    /* και ο πλήρης constructor */
    public PlayList(String name, Date creationdate) {
        this.playlistid = null;  // ετσι κι αλλιώς θα το ορίσει η ΒΔ
        this.name = name;
        this.creationdate = creationdate;
    }

    /* getters */
    public Integer getPlaylistid() { return playlistid; }
    public String getName() { return name; }
    public Date getCreationdate() { return creationdate; }

    /* setters εκτός ίσως από το primary key */
    public void setPlaylistid(Integer playlistid) { this.playlistid = playlistid; }
    public void setName(String name) { this.name = name; }
    public void setCreationdate(Date creationdate) { this.creationdate = creationdate; }

    @XmlTransient
    /* getter και setter για την songCollection */
    public Collection<Song> getSongCollection() {
        return songCollection;
    }
    public void setSongCollection(Collection<Song> songCollection) {
        this.songCollection = songCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (playlistid != null ? playlistid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PlayList)) {
            return false;
        }
        PlayList other = (PlayList) object;
        if ((this.playlistid == null && other.playlistid != null) || (this.playlistid != null && !this.playlistid.equals(other.playlistid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "rss_POJO.PlayList[ playlistid=" + playlistid + " ]";
    }
    
}